package com.xsky.datagrid.logic;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;


public class TableConfigLogic
{
	public static void main(String[] args)
	{
		TableConfigLogic logic = new TableConfigLogic();
		logic.createTableConfig("Staff");
	}

	public String createTableConfig(String tableName)
	{
		try
		{
			System.out.println("com/xsky/operation/vo/" + tableName+ ".hbm.xml");
			return this.configure(TableConfigLogic.class.getResourceAsStream("/com/xsky/operation/vo/" + tableName+ ".hbm.xml"));
		} catch (DocumentException e)
		{
			e.printStackTrace();
		} catch (IOException e)
		{
			e.printStackTrace();
		}
		return "";
	}

	private String configure(InputStream in) throws DocumentException,IOException{
		String text = this.getText(in);
		text = text.substring(text.indexOf("<hibernate-mapping>"));
		Document doc = DocumentHelper.parseText(text);
		String tableName = doc.getRootElement().element("class")
				.attributeValue("name").substring(doc.getRootElement().element("class").attributeValue("name").lastIndexOf(".") + 1);
		String result = "<table" + " name=\"" + tableName + "\""
				+ " title=\"" + tableName + "\"" + " resizeAble=\"true\""
				+ " editAble=\"false\"" + " width=\"-1\"" + " height=\"-1\""
				+ " percentWidth=\"100\"" + " percentHeight=\"100\"" + ">";
		String columns = "<columns addPanelTitle=\"\">";
		List propertyList = doc
				.selectNodes("//hibernate-mapping/class/property");
		for (int i = 0; i < propertyList.size(); i++)
		{
			Element columnElement = (Element) propertyList.get(i);
			String columnName = columnElement.attributeValue("name");
			String type = columnElement.attributeValue("type");
			String length = columnElement.element("column").attributeValue(
					"length");
			String notNull = columnElement.element("column").attributeValue(
					"not-null");
			String validateString = "&lt;data&gt;&lt;type&gt;"
					+ type.substring(type.lastIndexOf(".") + 1)
					+ "&lt;/type&gt;";
			if (notNull != null && "true".equals(notNull))
			{
				validateString += "&lt;required&gt;required&lt;/required&gt;";
			}
			if (length != null && !"".equals(length))
			{
				int intLength = (int) Math.floor(Integer.parseInt(length) / 3);
				validateString += "&lt;min&gt;0&lt;/min&gt;&lt;max&gt;"
						+ intLength + "&lt;/max&gt;";
			}
			validateString += "&lt;/data&gt;";
			columns += "<column " + " label=\"" + columnName + "\""
					+ " name=\"" + columnName + "\"" + " type=\"" + type + "\""
					+ " length=\""+length+"\"" 
					+ " width=\"\"" 
					+ " height=\"\"" 
					+ " visible=\"false\""
					+ " filterAble=\"true\"" + " validateAble=\"true\""
					+ " validateType=\"string\"" + " validator=\""
					+ validateString + "\"" + " searchAble=\"false\""
					+ " searchType=\"like\"" + " columnEditAble=\"false\""
					+ " editAble=\"true\"" + " autoCreate=\"false\""
					+ " createType=\"textInput\"" + " bandType=\"noband\""
					+ " bandSource=\"\"" + " dataField=\"\""
					+ " labelField=\"\"" + " rows=\"-1\"" 
					+ " editVisible=\"true\"" 
					+ " annotation=\"\"" 
					+ " />";
		}
		columns += "</columns>";
		result += columns;
		String rowsFunctionButtons = "";
		rowsFunctionButtons += "<rowsFunctionButtons width=\"200\" percentWidth=\"-1\" resizeAble=\"false\">";
//		rowsFunctionButtons += "<button type=\"linkButton\" label=\"修改\" icon=\"\" click=\"edit\" code=\"0\" toolTip=\"修改\" />";
//		rowsFunctionButtons += "<button type=\"linkButton\" label=\"删除\" icon=\"\" click=\"delete\" code=\"0\" toolTip=\"删除\" />";
		rowsFunctionButtons += "</rowsFunctionButtons>";
		result += rowsFunctionButtons;
		String functionButtons = "";
		functionButtons += "<functionButtons width=\"200\" percentWidth=\"-1\" resizeAble=\"false\">";
//		functionButtons += "<button type=\"button\" label=\"新增\" icon=\"\" click=\"add\" code=\"0\" toolTip=\"新增\" />";
//		functionButtons += "<button type=\"button\" label=\"删除选中\" icon=\"\" click=\"deleteSelected\" code=\"0\" toolTip=\"删除选中\" />";
		functionButtons += "</functionButtons>";
		result += functionButtons;
		result += "</table>";
		System.out.println(result);
		return "";

	}

	private String getText(InputStream in) throws IOException
	{
		ByteArrayOutputStream bout = new ByteArrayOutputStream();

		byte[] buffer = new byte[1024];

		int num = in.read(buffer);

		while (num > 0)
		{
			bout.write(buffer, 0, num);
			num = in.read(buffer);
		}
		return new String(bout.toByteArray(), "utf-8");
	}
}
